<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2021/4/9
 * Time: 17:03
 */

namespace app\admin\controller;

use app\common\model\JoinModel;
use think\Db;
use think\Validate;

class Join extends Common
{
    public function lists(){
        $list = Db::name('join_user')->field('id,username,nickname,region_name,create_time,is_lock')->order('id','desc')
            ->paginate(15,[]);
        return view('',['list'=>$list]);
    }
    public function addjoin(){
        if(Request()->isPost()) {
            $param = input('param.');
            $rule = [
                'username|电话号码'=>'require|mobile|max:12',
                'nickname|加盟商名字' =>'require|max:20',
                'region_id|管理区域' =>'require|number|max:11',
                'password|密码'=>'require|min:5|max:25'
            ];
            $validate = Validate::make($rule);

            $param['is_lock'] =   isset($param['is_lock'])?$param['is_lock']:0;

            if($param['is_lock'] ==='on'){
                $param['is_lock']  = 1;
            }
            $param['is_lock'] = intval($param['is_lock']);
            if (!$validate->check($param)) {
                return package(10002,$validate->getError());
            }
            $param['password'] = Pass( sha1($param['password']));

            $param['create_time'] = date("Y-m-d H:i:s");
            $param['region_name'] = Db::name('region')->where("id","=",$param["region_id"])->value("name");
            if(!$param["region_name"]){
                return package(10002,"管理区域未找到");
            }
            $m = new JoinModel();
            return checkRes($m->allowField(true)->save($param),'添加');
        }else{
            $region = Db::name('region')->where('open',1)
                ->field('id,name')
                ->select();
            return view('',['type'=>"addjoin","region"=>$region]);
        }
    }
    public function edit(){
        if(Request()->isPost()) {
            $param = input('param.');
            $id = intval($param['id']??0);
            if(!$id){
                return package(20001,'id错误');
            }
            $rule = [
                'username|电话号码'=>'require|mobile|max:12',
                'nickname|加盟商名字' =>'require|max:20',
                'region_id|管理区域' =>'require|number|max:11',
            ];
            $validate = Validate::make($rule);

            $param['is_lock'] =   isset($param['is_lock'])?$param['is_lock']:0;

            if($param['is_lock'] ==='on'){
                $param['is_lock']  = 1;
            }
            $param['is_lock'] = intval($param['is_lock']);
            if (!$validate->check($param)) {
                return package(10002,$validate->getError());
            }
            if(isset($param['password']) && !$param['password']){
                unset($param['password']);
            }else{
                $param['password'] =  Pass( sha1($param['password']));
            }
            $param['create_time'] = date("Y-m-d H:i:s");
            $param['region_name'] = Db::name('region')->where("id","=",$param["region_id"])->value("name");
            if(!$param["region_name"]){
                return package(10002,"管理区域未找到");
            }
            $m = new JoinModel();
            return checkRes($m->allowField(true)->save($param,['id'=>$id]),'修改');
        }else {
            $region = Db::name('region')->where('open', 1)
                ->field('id,name')
                ->select();
            $id = input('param.id/d', 0);
            $info = JoinModel::field('id,username,nickname,is_lock,region_id')->get($id);
            return view('addjoin', ['type' => "edit", "region" => $region, 'info' => $info]);
        }
    }
    public function del($id){
        $result =Db::name('join_user')->where('id',$id)->delete();
        return checkRes($result,'删除');
    }
}